package org.itboys.mobile.service.mongo.mobile;

import org.itboys.mobile.entity.mongo.OrderShopGift;
import org.itboys.mobile.entity.mongo.ShopGift;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.mongodb.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created on 2017/10/30.
 * @author 朱坤涛
 */
@Service
public class OrderShopGiftService extends BaseService<OrderShopGift,Long> {
    @Autowired
    private ShopGiftService shopGiftService;

    @Resource(name = "adminDS")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<OrderShopGift> getEntityClass() {
        return OrderShopGift.class;
    }

    public void delivery(Long id,String express,String expressNo,String memo){
        OrderShopGift order = getById(id);
        order.setStatus(2);
        order.setExpress(express);
        order.setExpressNo(expressNo);
        order.setSendTime(new Date());
        order.setExpressEng(memo);
        update(order);
    }
    //购物车提交订单
    public  OrderShopGift createOrder(Long id, SundayAddress address){
        ShopGift gift = shopGiftService.getById(id);
        //生成主订单
        String no = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        //保存Order
        OrderShopGift order = new OrderShopGift();
        order.setMemberId(address.getMemberId());
        order.setOrderNo("GNO"+no);
        order.setStatus(0);
        order.setTotalFee(gift.getAmount());
        order.setCreateTime(new Date());
        order.setAddress(address.getProvinceName()+address.getCityName()+address.getDistrictName()+address.getReceiveAddress());
        order.setName(address.getReceiveName());
        order.setMobile(address.getReceiveMobile());
        order.setProvinceId(address.getProvinceId());
        order.setDistrictId(address.getDistrictId());
        order.setCityId(address.getCityId());
        order.setRefId(id);
        save(order);
        return order;

    }

    public OrderShopGift updateOrder(Long orderId,String mobile,String name,String phone, Long provinceId, Long cityId, Long districtId, String address,String provinceName,String cityName,String districtName){
        OrderShopGift order = getById(orderId);
        order.setAddress(provinceName+cityName+districtName+address);
        order.setName(name);
        order.setMobile(phone);
        order.setProvinceId(provinceId);
        order.setDistrictId(districtId);
        order.setCityId(cityId);
        order.setUserMobile(mobile);
        update(order);
        return order;
    }

}
